﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Model.DbModels.Common;
using TomNet.IMSC.Web.Common;
using TomNet.IMSC.Web.Filters;
using TomNet.IMSC.Web.Models.ApiParams;
using TomNet.Web.Mvc.UI;
using TomNet.Web.WebApi;

namespace TomNet.IMSC.Web.Controllers.StaffAPI
{
    [Description("API-跟进信息")]
    [ApiAuthentication]
    public class StaffMessageBoardsInterfaceController : BaseApiController
    {
        public IMessageBoardsContract MessageBoardsContract { get; set; }
        public IStaffContract StaffContract { get; set; }
        public IHttpActionResult ttGetMessageBoardsInterface([FromBody]MessageBoardsModel entity)
        {
            var query = from m in MessageBoardsContract.Entities.Where(n => !n.IsDeleted && n.MessageBoardsType == entity.MessageBoardsType && n.InterfixID == entity.InterfixID)
                        select new MessageBoardsListModel
                        {
                            Id = m.Id,
                            MessageBoardsType = m.MessageBoardsType,
                            StaffID = StaffContract.TrackEntities.Where(s => !s.IsDeleted && s.Id == m.StaffID).Select(s => s.Name).FirstOrDefault(),
                            InterfixID = m.InterfixID,
                            Content = m.Content,
                            CreatedTime = m.CreatedTime,
                        };
            var a = query.ToList();
            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.CreatedTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)
                : query;

            var result = new AjaxResultRow("成功", AjaxResultType.Success, query.ToList(), total);
            return Token(result, "");
        }

        public IHttpActionResult ttMessageBoardsInsert([FromBody]MessageBoards entity)
        {
            var userid = BaseWebApiExt.GetUserIDByToken();//获取token中userid

            try
            {
                MessageBoards mb = new MessageBoards()
                {
                    MessageBoardsType = entity.MessageBoardsType,
                    StaffID = userid,
                    InterfixID = entity.InterfixID,
                    Content = entity.Content

                };
                var count = MessageBoardsContract.Insert(mb);
                if (count > 0)
                {
                    var result = new AjaxResult("添加成功", AjaxResultType.Success);
                    return Token(result, "");
                }
                else
                {
                    var result = new AjaxResult("添加失败", AjaxResultType.Error);
                    return Token(result, "");
                }
            }
            catch (Exception e)
            {
                var result = new AjaxResult("添加失败: " + e.Message, AjaxResultType.Error);
                return Token(result, "");
            }
        }
    }
}